<?php

namespace App\Http\Controllers;

use App\Constant\Error;
use App\Http\Controllers\WechatController;
use App\Models\Apply;
use App\Service\ApplyService;
use Cache;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ApplyRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;


/**
 * 报名控制器
 * Class ApplyController
 * @package App\Http\Controllers
 *
 *
 * 此为资源控制器，同一处理行为如下：
 * 动词    路径    行为（方法）    路由名称
 * GET    /apply    index    apply.index
 * GET    /apply/create    create    apply.create
 * POST    /apply    store    apply.store
 * GET    /apply/{apply}    show    apply.show
 * GET    /apply/{apply}/edit    edit    apply.edit
 * PUT/PATCH    /apply/{apply}    update    apply.update
 * DELETE    /apply/{apply}    destroy    apply.destroy
 *
 */
class ApplyController extends Controller
{
    private $apply;

    function __construct(ApplyService $apply)
    {
        $this->middleware('inwechat');
        $this->apply = $apply;
    }

    /**
     * 报名首页
     * /apply
     * @author linyi
     *
     * @return [type]     [description]
     */
    public function index()
    {
        $openid = getOpenid();
        $count = Apply::where('openid', $openid)->whereDate('created_at', '>', date('Y-m-d', strtotime('-1 month')))->count();
        if ($count > 0) {
            abort(500, Error::APPLY_OPENID_LIMIT_MSG);
        }

        //视图文件：resources/views/apply/index.blade.php
        Log::notice('访问报名页面 openid:' . getOpenid());
        return view('apply.index');
    }

    /**
     * 添加报名视图
     * /apply/create
     * @author linyi
     *
     * @return [type]                   [description]
     */
    public function create()
    {
        return view('apply.create');
    }

    /**
     * 添加报名
     * @author linyi
     *
     * @param  ApplyRequest $request [description]
     * @return [type]                            [description]
     */
    public function store(ApplyRequest $request)
    {
        $r = $this->apply->storeApply($request);
        Log::notice('报名结果 openid:' . getOpenid() . ' return:' . json_encode($r));
        return response()->json($r);
        //展示成功页
        //return view('apply.success');
    }

    /**
     *
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        $apply = $this->apply->getApplyById($id);
        return response()->json($apply);
        return view('apply.edit')->with(compact('apply'));
    }

    /**
     * 修改报名视图
     * @author linyi
     *
     * @param  [type]                   $id [description]
     * @return [type]                       [description]
     */
    public function edit($id)
    {
        $apply = $this->apply->getApplyById($id);
        return view('apply.edit')->with(compact('apply'));
    }

    /**
     * 修改报名
     * @author linyi
     *
     * @param  TicketRequest $request [description]
     * @param  [type]                   $id      [description]
     * @return [type]                            [description]
     */
    public function update(ApplyRequest $request, $id)
    {
        $this->apply->updateApply($request->all(), $id);
        return view('apply.success');
    }
}
